package cn.csxw.openchina.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.csxw.openchina.R;
import cn.csxw.openchina.base.BaseActivity;
import cn.csxw.openchina.entity.DrawerMenuBean;
import cn.csxw.openchina.ui.adapter.DrawerMenuAdapter;
import cn.csxw.openchina.ui.adapter.MainAdapter;
import cn.csxw.openchina.ui.fragment.DiscoverFragment;
import cn.csxw.openchina.ui.fragment.MoveFragment;
import cn.csxw.openchina.ui.fragment.MyFragment;
import cn.csxw.openchina.ui.fragment.SyntheticallyFragment;
import cn.csxw.openchina.ui.view.MyDialog;

/**
 *
 */
public class MainActivity extends BaseActivity {

    @BindView(R.id.home_vp)
    ViewPager mViewPager;
    @BindView(R.id.menu_news)
    TextView mMenuNews;
    @BindView(R.id.menu_tweet)
    TextView mMenuTweet;
    @BindView(R.id.menu_add)
    ImageView mMenuAdd;
    @BindView(R.id.menu_find)
    TextView mMenuFind;
    @BindView(R.id.menu_me)
    TextView mMenuMe;
    //当前选中项目
    private TextView currentSelectedMenu  ;
    private DrawerLayout mDrawer;
    private ActionBarDrawerToggle mToggle;
    public Toolbar mToolbar;

    private int[] mDrawables = new int[]{R.drawable.drawer_menu_icon_quest_nor,
            R.drawable.drawer_menu_icon_opensoft_nor,R.drawable.drawer_menu_icon_blog_nor,
            R.drawable.drawer_menu_icon_gitapp_nor};
    private String[] titles = new String[]{"技术问答", "开源软件", "博客区", "Git客户端"};
    private List<DrawerMenuBean> mMenuBeenList = new ArrayList<>();
    @Override
    public int getLayoutRes() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        initViewPager();
        initToolBar();   //  初始化ToolBar
        initMenuBean();  //  初始化菜单栏的JavaBean
        setListView();   //  设置ListView
    }

    /**
     * 初始化菜单栏的JavaBean
     */
        private void initMenuBean() {
        for (int i = 0; i < mDrawables.length; i++) {
            DrawerMenuBean menuBean = new DrawerMenuBean(titles[i], mDrawables[i]);
            mMenuBeenList.add(menuBean);
        }
    }

    /**
     * 设置ListView
     */
    private void setListView() {
        ListView listView = (ListView) findViewById(R.id.lv_drawer_left);
        listView.setAdapter(new DrawerMenuAdapter(this,mMenuBeenList));
    }

    /**
     * 设置ToolBar
     */

    private void initToolBar() {
        mToolbar = (Toolbar) findViewById(R.id.tb_home_toolbar);
        mToolbar.setTitleTextColor(Color.WHITE);    //  设置标题颜色为白色
        mToolbar.setOverflowIcon(ContextCompat.getDrawable(this,R.drawable.actionbar_back_icon_normal));

        setSupportActionBar(mToolbar);    // Activity和ToolBar关联一起
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);    // 设置导航按钮显示出来
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(true);   //设置显示标题

        mDrawer = (DrawerLayout) findViewById(R.id.left_DrawerLayout);
        mToggle = new ActionBarDrawerToggle(this, mDrawer, R.string.open, R.string.close);
        mToggle.syncState();     //该方法会自动和actionBar关联
    }

    /**
     * 初始化ViewPager
     */
    private void initViewPager() {
        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(new SyntheticallyFragment());
        fragmentList.add(new MoveFragment());
        fragmentList.add(new DiscoverFragment());
        fragmentList.add(new MyFragment());
        MainAdapter adapter = new MainAdapter(getSupportFragmentManager(), fragmentList);
        mViewPager.setAdapter(adapter);

        currentSelectedMenu =mMenuNews;  //  默认选中综合页面
        onPagerSelected(currentSelectedMenu,0);
    }

    //添加监听器
    @Override
    public void initListener() {
        //  菜单键的点击事件
        mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                Intent intent = new Intent(MainActivity.this, SearchActivity.class);
                startActivity(intent);
                return false;
            }
        });

        // DrawerLayout 和 ToolBar进行关联
        mDrawer.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                mToggle.onDrawerSlide(drawerView,slideOffset);
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                mToggle.onDrawerOpened(drawerView);
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                mToggle.onDrawerClosed(drawerView);
            }

            @Override
            public void onDrawerStateChanged(int newState) {
                mToggle.onDrawerStateChanged(newState);
            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    public void onClick(View v, int id) {
        switch (id) {
            //新闻按钮
            case R.id.menu_news:
                onPagerSelected(v,0);
                break;
            //动弹按钮
            case R.id.menu_tweet:
                onPagerSelected(v,1);
                break;
            //发现按钮
            case R.id.menu_find:
                onPagerSelected(v,2);
                break;
            //我按钮
            case R.id.menu_me:
                onPagerSelected(v,3);
                break;
            case R.id.menu_add :
                AddClick();  //  加号的点击事件
                break;
        }
    }

    /*public void onClick(View view) {
        AddClick();
    }*/
    /**
     * 底部加号的点击事件
     */
    private void AddClick() {
        final MyDialog dialog = new MyDialog(this);
        dialog.show();
        dialog.getWindow().setWindowAnimations(R.style.AnimBottom); //  dialog由下往上出现的动画
        dialogListening(dialog);   //  设置dialog点击文字,相片,拍照,关闭的事件
    }

    //ViewPager与按钮关联方法
    private void onPagerSelected(View menuButton,int position){
        //如果已经被选中了就return掉
        if(menuButton.isSelected()){
            return;
        }
        mViewPager.setCurrentItem(position, false);  //  取消ViewPager切换动画
        currentSelectedMenu.setSelected(false);
        switch (position){
            case 0:
                currentSelectedMenu =mMenuNews;
                break;
            case 1:
                currentSelectedMenu =mMenuTweet;
                break;
            case 2:
                currentSelectedMenu =mMenuFind;
                break;
            case 3:
                currentSelectedMenu =mMenuMe;
                break;
        }
        currentSelectedMenu.setSelected(true);
    }

    /**
     * 设置dialog点击文字,相片,拍照,关闭的回调
     * @param dialog
     */
    private void dialogListening(final MyDialog dialog) {
        dialog.setOnClickItemListening(new MyDialog.OnClickItemListening() {
            @Override
            public void clickWrite() {
                Toast.makeText(MainActivity.this, "文字", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void clickPhoto() {
                Toast.makeText(MainActivity.this, "相片", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void clickTackPicture() {
                Toast.makeText(MainActivity.this, "拍照", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void clickClose() {
                dialog.cancel();     //  关闭自定义dialog
            }
        });
    }

    /**
     *  点击ToolBar左上角的图标会调用该方法
     * @param item
     * @return
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        boolean drawerOpen = mDrawer.isDrawerOpen(Gravity.LEFT);
        if (drawerOpen) {
            mDrawer.closeDrawer(Gravity.LEFT);    //  关闭侧边栏
        }else {
            mDrawer.openDrawer(Gravity.LEFT);    //   打开侧边栏
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     *   右上角搜索的布局
     * @param menu
     * @return
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.search_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

}
